<?

if(isset($_POST["pass"]))
    if($Users->pass($_POST["course"],$_POST["student"],$_POST["grade"],$_POST["group"]))
        header("Location: /?page=taught");

if(isset($_POST["insert_lecture"]))
    if(($value = $Lectures->insert($_POST["instance"],$_POST["date"],$_POST["start_time"],$_POST["end_time"],$_POST["location"])) === true)
        header("Location: /?page=taught");
    else
        $error .= $value;

if(isset($_POST["update_lecture"]))
    if(($value = $Lectures->update($_POST["id"],$_POST["date"],$_POST["start_time"],$_POST["end_time"],$_POST["location"])) === true)
        header("Location: /?page=taught");
    else
        $error .= $value;

if(isset($_POST["delete_lecture"]))
    if($Lectures->delete($_POST["id"]))
        header("Location: /?page=taught");

if(isset($_POST["insert_exam"]))
    if(($value = $Exams->insert($_POST["instance"],$_POST["date"],$_POST["start_time"],$_POST["end_time"],$_POST["location"])) === true)
        header("Location: /?page=taught");
    else
        $error .= $value;

if(isset($_POST["update_exam"]))
    if(($value = $Exams->update($_POST["id"],$_POST["date"],$_POST["start_time"],$_POST["end_time"],$_POST["location"])) === true)
        header("Location: /?page=taught");
    else
        $error .= $value;

if(isset($_POST["delete_exam"]))
    if($Exams->delete($_POST["id"]))
        header("Location: /?page=taught");

if(isset($_POST["insert_group"]))
    if($Groups->insert($_POST["instance"],$_POST["teacher"]))
        header("Location: /?page=taught");

if(isset($_POST["update_group"]))
    if($Groups->update($_POST["group"],$_POST["teacher"]))
        header("Location: /?page=taught");

if(isset($_POST["delete_group"]))
    if($Groups->delete($_POST["group"]))
        header("Location: /?page=taught");

?>
<div id="content">
    <h2>Opettamani kurssit</h2>
    <? echo $error; ?>
    <ul>
        <? $taughts = $Courses->get_taught_by($user);
        foreach($taughts as &$taught) { ?>
            <li>
                <h3><a href="javascript:void(0);" onclick="toggle(this)" class="toggle"><? echo e($taught->name); ?></a></h3>
                <ul>
                    <li>
                        <h4>Luennot</h4>
                        <table class="timetable">
                            <thead>
                                <tr>
                                    <th class="number">#</th>
                                    <th class="date">Päivä</th>
                                    <th class="start_time">Alkaa</th>
                                    <th class="end_time">Päättyy</th>
                                    <th class="location">Paikka</th>
                                    <th class="reset"></th>
                                    <th class="update"></th>
                                    <th class="delete"></th>
                                </tr>
                            </thead>
                            <tbody>
                                <? $lectures = $Lectures->get_by_instance($taught->instance);
                                $i = 0;
                                $date = null;
                                $start_time = null;
                                $end_time = null;
                                $location = null;
                                foreach($lectures as &$lecture)
                                {
                                    ++$i;
                                    $id = e($lecture->id);
                                    $date = date('d.m.Y',$lecture->date);
                                    $start_time = date('H:i',$lecture->start_time);
                                    $end_time = date('H:i',$lecture->end_time);
                                    $location = e($lecture->location);
                                    ?>
                                    <tr>
                                        <form method="post" action="?page=taught">
                                            <input type="hidden" name="id" value="<? echo $id; ?>" />
                                            <td class="number"><? echo $i; ?></td>
                                            <td class="date"><input type="text" name="date" maxlength="5" value="<? echo $date; ?>" /></td>
                                            <td class="start_time"><input type="text" name="start_time" maxlength="5" value="<? echo $start_time; ?>" /></td>
                                            <td class="end_time"><input type="text" name="end_time" maxlength="5" value="<? echo $end_time; ?>" /></td>
                                            <td class="location"><input type="text" name="location" value="<? echo $location; ?>" /></td>
                                            <td class="reset"><input type="reset" name="reset" value="Kumoa" /></td>
                                            <td class="update"><input type="submit" name="update_lecture" value="Muokkaa" /></td>
                                            <td class="delete"><input type="submit" name="delete_lecture" value="Poista" /></td>
                                        </form>
                                    </tr>
                                <? } if($date != null) $date = date('d.m.Y',strtotime($date)+7*24*60*60); ?>
                                <tr>
                                    <form method="post" action="?page=taught">
                                        <input type="hidden" name="instance" value="<? echo e($taught->instance); ?>" />
                                        <td class="number"></td>
                                        <td class="date"><input type="text" name="date" maxlength="10" value="<? echo $date; ?>" /></td>
                                        <td class="start_time"><input type="text" name="start_time" maxlength="5" value="<? echo $start_time ?>" /></td>
                                        <td class="end_time"><input type="text" name="end_time" maxlength="5" value="<? echo $end_time; ?>" /></td>
                                        <td class="location"><input type="text" name="location" value="<? echo $location; ?>" /></td>
                                        <td class="reset"><input type="reset" name="reset" value="Kumoa" /></td>
                                        <td class="update"><input type="submit" name="insert_lecture" value="Lisää" /></td>
                                        <td class="delete"></td>
                                    </form>
                                </tr>
                            </tbody>
                        </table>
                    </li>
                    <li>
                        <h4>Kokeet</h4>
                        <table class="timetable">
                            <thead>
                                <tr>
                                    <th class="number">#</th>
                                    <th class="date">Päivä</th>
                                    <th class="start_time">Alkaa</th>
                                    <th class="end_time">Päättyy</th>
                                    <th class="location">Paikka</th>
                                    <th class="reset"></th>
                                    <th class="update"></th>
                                    <th class="delete"></th>
                                </tr>
                            </thead>
                            <tbody>
                                <? $exams = $Exams->get_by_instance($taught->instance); $i = 0;
                                foreach($exams as &$exam)
                                {
                                    ++$i;
                                    $id = e($exam->id);
                                    $date = date('d.m.Y',$exam->date);
                                    $start_time = date('H:i',$exam->start_time);
                                    $end_time = date('H:i',$exam->end_time);
                                    $location = e($exam->location);
                                    ?>
                                    <tr>
                                        <form method="post" action="?page=taught">
                                            <input type="hidden" name="id" value="<? echo $id; ?>" />
                                            <td class="number"><? echo $i; ?></td>
                                            <td class="date"><input type="text" name="date" maxlength="5" value="<? echo $date; ?>" /></td>
                                            <td class="start_time"><input type="text" name="start_time" maxlength="5" value="<? echo $start_time; ?>" /></td>
                                            <td class="end_time"><input type="text" name="end_time" maxlength="5" value="<? echo $end_time; ?>" /></td>
                                            <td class="location"><input type="text" name="location" value="<? echo $location; ?>" /></td>
                                            <td class="reset"><input type="reset" name="reset" value="Kumoa" /></td>
                                            <td class="update"><input type="submit" name="update_exam" value="Muokkaa" /></td>
                                            <td class="delete"><input type="submit" name="delete_exam" value="Poista" /></td>
                                        </form>
                                    </tr>
                                <? } $date = date('d.m.Y',strtotime($date) + 7 * 24 * 60 * 60); ?>
                                <tr>
                                    <form method="post" action="?page=taught">
                                        <input type="hidden" name="instance" value="<? echo e($taught->id); ?>" />
                                        <td class="number"></td>
                                        <td class="date"><input type="text" name="date" maxlength="10" value="" /></td>
                                        <td class="start_time"><input type="text" name="start_time" maxlength="5" value="" /></td>
                                        <td class="end_time"><input type="text" name="end_time" maxlength="5" value="" /></td>
                                        <td class="location"><input type="text" name="location" value="" /></td>
                                        <td class="reset"><input type="reset" name="reset" value="Kumoa" /></td>
                                        <td class="update"><input type="submit" name="insert_exam" value="Lisää" /></td>
                                        <td class="delete"></td>
                                    </form>
                                </tr>
                            </tbody>
                        </table>
                    </li>
                    <li>
                        <h4>Ryhmät</h4>
                        <ol>
                            <? $groups = $Groups->get_by_instance($taught->instance);
                            $supervisers = $Groups->get_potential_supervisers($taught->instance);
                            foreach($groups as &$group) { ?>
                                <li>
                                    <form  method="post" action="?page=taught">
                                    <input type="hidden" name="group" value="<? echo e($group->id); ?>" />
                                        <select name="teacher" >
                                            <option value="">Ei valvojaa</option>
                                            <?
                                            foreach($supervisers as &$superviser)
                                            {
                                                if($superviser->id == $group->superviser_id)
                                                    $selected = "selected='selected'";
                                                else
                                                    $selected = null;
                                                ?>
                                                    <option value="<? echo e($superviser->id); ?>" <? echo $selected; ?>><? echo e($superviser->name); ?></option>
                                            <? } ?>
                                        </select>
                                        <input type="submit" name="update_group" value="Vaihda valvojaa" />
                                        <!-- input type="submit" name="delete_group" value="Poista ryhmä" / -->
                                    </form>
                                    <ul>
                                        <li>
                                            <h6>Tapaamiset</h6>
                                            <table class="timetable">
                                                <thead>
                                                    <tr>
                                                        <th class="number">#</th>
                                                        <th class="date">Päivä</th>
                                                        <th class="time">Aika</th>
                                                        <th class="location">Paikka</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <? $meetings = $Meetings->get_by_group($group->id); $i = 0;
                                                    foreach($meetings as &$meeting) { ++$i; ?>
                                                        <tr>
                                                            <td class="number"><? echo $i; ?></td>
                                                            <td class="date"><? echo date("d.m.Y",$meeting->date); ?></td>
                                                            <td class="time"><? echo date("H:i",$meeting->start_time); ?>—<? echo date("H:i",$meeting->end_time); ?></td>
                                                            <td class="location"><? echo e($meeting->location); ?></td>
                                                        </tr>
                                                    <? } ?>
                                                </tbody>
                                            </table>
                                        </li>
                                        <li>
                                            <h6>Opiskelijat</h6>
                                            <table class="students">
                                                <thead>
                                                    <tr>
                                                        <th class="number">#</th>
                                                        <th class="name">Nimi</th>
                                                        <th class="pass"></th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <? $students = $Groups->get_members($group->id); $i = 0;
                                                    foreach($students as &$student) { ++$i; ?>
                                                        <tr>
                                                            <td class="number"><? echo $i; ?></td>
                                                            <td class="name"><? echo e($student->name); ?></td>
                                                            <td class="pass">
                                                                <form method="post" action="?page=taught">
                                                                    <select name="grade">
                                                                        <option value="0">Hylätty</option>
                                                                        <option value="">Hyväksytty</option>
                                                                        <option value="1">1</option>
                                                                        <option value="2">2</option>
                                                                        <option value="3">3</option>
                                                                        <option value="4">4</option>
                                                                        <option value="5">5</option>
                                                                    </select>
                                                                    <input type="hidden" name="group" value="<? echo e($group->id); ?>"/>
                                                                    <input type="hidden" name="student" value="<? echo e($student->id); ?>"/>
                                                                    <input type="hidden" name="course" value="<? echo e($taught->id); ?>"/>
                                                                    <input type="submit" name="pass" value="Päästä kurssilta arvosanalla" />
                                                                </form>
                                                            </td>
                                                        </tr>
                                                    <? } ?>
                                                </tbody>
                                            </table>
                                        </li>
                                    </ul>
                                </li>
                            <? } ?>
                            <li>
                                <form  method="post" action="?page=taught">
                                    <input type="hidden" name="instance" value="<? echo e($taught->instance); ?>" />
                                    <select name="teacher" >
                                        <option value="">Ei valvojaa</option>
                                        <? foreach($supervisers as &$superviser) { ?>
                                            <option value="<? echo e($superviser->id); ?>"><? echo e($superviser->name); ?></option>
                                        <? } ?>
                                    </select>
                                    <input type="submit" name="insert_group" value="Luo uusi ryhmä" />
                                </form>
                            </li>
                        </ol>
                    </li>
                </ul>
            </li>
        <? } ?>
    </ul>
</div>
